<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\DB;

use QL\QueryList;

class CollectController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.collect.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        // 设置执行时间限制 0为不限制时间
        set_time_limit(0);

        $ql = QueryList::get($request->url)->removeHead();

        // 采集表的每行内容
        $tableRows = $ql->find('.tablelist tr:gt(0)')->map(function ($row) {
            return $row->find('td')->texts()->all();
        });

        // 采集表中链接
        $url = $ql->find('.tablelist tr:gt(0)')->map(function ($row) {
            return $row->find('td>a')->attr('href');
        });

        for ($i = 0; $i < count($tableRows); $i++) {

            /*
             * $tableRows[$i][0] 服务器名称
             * $tableRows[$i][1] 服务器IP
             * $tableRows[$i][2] 开机时间[月/日/时]
             * $tableRows[$i][3] 线路类别
             * $tableRows[$i][4] 详细版本介绍
             * $tableRows[$i][5] 客服QQ
             * $tableRows[$i][6] 游戏主页
             */
            if (isset($tableRows[$i][2])) {     //如果服务器IP信息存在

                $type = ''.mt_rand(0, 1).','.mt_rand(0, 1).','.mt_rand(0, 1).','.mt_rand(0, 1).','.mt_rand(0, 1).'';
                $open_time = date('Y/m/'.substr($tableRows[$i][2],2, 2).' '.mt_rand(7, 12).':00:00');

                DB::table('game_data')->updateOrInsert(
                    ['url' => $url[$i]],
                    ['type' => $type,
                        'server_name' => $tableRows[$i][0],
                        'server_ip' => $tableRows[$i][1],
                        'open_time' => $open_time,
                        'network' => 'null',
                        'introduction' => substr($tableRows[$i][3], 0, -7),
                        'qq' => 'null',
                        'url' => $url[$i],
                        'audit' => 1,
                        'count' => mt_rand(1, 3),
                        'consume' => 0,
                        'publisher' => Auth::user()->username,
                        'release_date' => date('Y-m-d H:i:s')
                    ]);

            }
        }

        return response()->json(['message' => '采集成功。', 'success' => 'true']);

    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

}
